package view;

import util.Controller;

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Map;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class Card extends JLabel implements MouseListener{

    private boolean inCooling;
    private int cd;
	private String cardName;
	private int price;
    private ImageIcon card;
	private ImageIcon card1;
	private ImageIcon card2;
	private ImageIcon card3;
	private ImageIcon preImg;
	private ImageIcon blurImg;
	private JFrame frame;
	private int cardWidth;
	private int cardHeight;
	private Map<String, Plant>plantMap;
	private Controller controller;
	private int y;
	
    Card(JFrame frame , String name, Map<String, Plant>plantMap, Controller controller){
    	this.controller = controller;
    	this.plantMap = plantMap;
	    this.cardName = name ;
	    this.frame = frame;
	    this.card1 = new ImageIcon("Img\\Cards\\"+cardName+"0.png");
	    this.card2 = new ImageIcon("Img\\Cards\\"+cardName+"1.png");
		this.card3 = new ImageIcon("Img\\Cards\\"+cardName+"2.png");
		this.preImg = new ImageIcon("img\\"+cardName+"\\"+cardName+"_0.png ");
		this.blurImg = new ImageIcon("img\\Blurs\\"+cardName+".png ");
	    this.card = card2;
	    this.inCooling = false;
	    this.addMouseListener(this);
	    this.cardHeight = card1.getIconHeight();
	    this.cardWidth = card1.getIconWidth();
	    this.price = plantMap.get(cardName).getPrice();
	    this.y = card.getIconHeight();
    }

	public void check(int SunCount){
		if (SunCount>=plantMap.get(this.cardName).getPrice()&&!inCooling){
			card = card1;
			this.repaint();
		}else{
			if (!inCooling){
				card = card2;
			}
		}
	}

	public int getPrice() {
		return price;
	}

	public void setInCooling(boolean inCooling) {
		this.inCooling = inCooling;
	}

	public void coolDown(){

	}

    public String getCardName() {
        return cardName;
    }

    public void picChange(){
        if (card.equals(card1))
            card = card3;
        else
            card = card1;
        this.repaint();
    }

	public int getCardWidth() {
		return cardWidth;
	}

	public int getCardHeight() {
		return cardHeight;
	}

	@Override
	public void paintComponent(Graphics g) {
           super.paintComponent(g);
           g.drawImage(card.getImage()
                   ,0, 0,card.getIconWidth(), card.getIconHeight(), this);

           g.drawImage(card2.getImage()
				,0, y,card.getIconWidth(), card.getIconHeight(), this);
       }

	@Override
	public void mouseClicked(MouseEvent e) {
        if (!inCooling &&controller.getSun()>=this.getPrice()){
        	controller.getTopPanel().setVisible(true);
        	controller.setPreImg(this.preImg);
        	controller.setBlurImg(this.blurImg);
        	controller.getCard(this);
            picChange();
            frame.setCursor(Frame.DEFAULT_CURSOR);
        }
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		 frame.setCursor(Frame.HAND_CURSOR);
	}

	@Override
	public void mouseExited(MouseEvent e) {		
		frame.setCursor(Frame.DEFAULT_CURSOR);	
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub
	}

}
